const Base = require('./base.js');
module.exports = class extends Base {
    /**
     * index action
     * @return {Promise} []
     */
    async indexAction() {
        const page = this.get('page') || 1;
        const size = this.get('size') || 20;
        const sortField = this.get('sortfield') || 'id';
        const shelves_beg = this.get('begin') || '';
        const shelves_end = this.get('end') || '';
        const title = this.get('title') || '';
        let seachdata = { id: ['>', 0]}, timesearch = {};
        if (title) {
            seachdata.title = ['like', `%${title}%`];
        }

        if (shelves_beg || shelves_end) {
            if (shelves_beg) {
              timesearch['egt'] = shelves_beg;
            }
            if (shelves_end) {
              timesearch['elt'] = shelves_end;
            }
            seachdata.update_time = timesearch;
        }

        const model = this.model('graphic');
        const data = await model.where(
            seachdata
        ).order([sortField]).page(page, size).countSelect();
        for(const item of data.data){
            item.looktimes= await this.model('graphic_join').where({graphic_id:item.id,type:'join'}).sum('times');
        }
        return this.success(data);
    }

    async saveAction() {
        const model = this.model('graphic');
        let values = this.post();
        let id = this.post('id')||0;
        if (id>0) {
            id=await model.where({ id: id }).update(values);
        } else {
            values['id']=0;
            id = await model.add(values);
            values['id'] = id;
        }
        return this.success(values);
    }
    async updateAction() {
        const model = this.model('graphic');
        const values = this.post();
        const id = this.post('id');
        let data = await model.where({id: id}).update(values);
        return this.success(data);
    }

    async readAction() {
        const model = this.model('graphic');
        const id = this.get('id');
        const data=await model.where({id: id}).find();
        return this.success(data);
    }

    async deleteAction() {
        const model = this.model('graphic');
        const id = this.post('id');
        let data = await model.where({ id: id }).delete();
        return this.success(data);
    }
     /**
     * 任务激活
     */
    async activeAction() {
        const model = this.model('graphic');
        const values = this.post();
        const id = this.post('id');
        values['is_delete'] = 0;
        let data = await model.where({id: id}).update(values);
        return this.success(data);
    }
    /**
     * 任务冻结
     */
    async freezeAction() {
        const model = this.model('graphic');
        const id = this.post('id');
        let data = await model.where({ id: id }).update({is_delete:1});
        return this.success(data);
    }

     /**
     * 获取已激活任务
     */
    async getActivegraphicAction() {
        const model = this.model('graphic');
        let data = await model.where({ is_delete: 0 }).select();
        return this.success(data);
    }

    async lookUserAction() {//浏览用户
        const model = this.model('graphic_join');
        const page = this.get('page') || 1;
        const size = this.get('size') || 20;
        const taskid=this.get('id') || 0;
    //      const sql = 'SELECT u.id, u.name, u.nickname, u.gender,u.city FROM hiolabs_user as u ' +
    //         'right JOIN hiolabs_graphic_join as jo on u.id = jo.user_id ' +
    //         ' where u.gender is not null'+
    //         ' group by jo.user_id ORDER BY jo.user_id';
    // let datas = await this.model().query(sql);
    const datas = await model.join({ table: 'user',  join: 'left', as:'u', on: ['user_id', 'id'] })
    .where( {graphic_id:taskid}).field("u.id as userid, u.name, u.gender,u.city,u.mobile, u.nickname").group('hiolabs_graphic_join.user_id').page(page, size).countSelect();
    for(const item of datas.data){
        item.nickname = Buffer.from(item.nickname, 'base64').toString();

    }

        return this.success(datas);
    }

    async shareStaticAction() {//分享统计
        const model = this.model('graphic_join');
        const integral_model = this.model('task_integral');
        const task_m = this.model('task');
        const page = this.get('page') || 1;
        const size = this.get('size') || 20;
        const graphic_id=this.get('id') || 0;
    //      const sql = 'SELECT u.id, u.name, u.nickname, u.gender,u.city FROM hiolabs_user as u ' +
    //         'right JOIN hiolabs_graphic_join as jo on u.id = jo.user_id ' +
    //         ' where u.gender is not null'+
    //         ' group by jo.user_id ORDER BY jo.user_id';
    // let datas = await this.model().query(sql);
    const datas = await model.join({ table: 'user',  join: 'left', as:'u', on: ['shareuser_id', 'id'] })
    .where( {'hiolabs_graphic_join.graphic_id':graphic_id,type:'join'}).field("hiolabs_graphic_join.create_time,u.id as userid, u.nickname").group('u.id').page(page, size).countSelect();
    const task = await task_m.where({custom:1,source_type:41,source_id:graphic_id}).find();
    for(const item of datas.data){
        item.nickname = Buffer.from(item.nickname, 'base64').toString();
        item.clickcount=await model.where( {graphic_id:graphic_id,type:'join',shareuser_id:item.userid}).sum('times');
        item.usercount=await model.where( {graphic_id:graphic_id,type:'join',shareuser_id:item.userid}).count();//点击用户数
        item.rewrad= await integral_model.where({task_id:task.id,user_id:item.userid}).sum('integral');
        item.rewrad=item.rewrad?item.rewrad:0;
    }

        return this.success(datas);
    }
};